package com.smf.lsc.msrf.dubbo.filter;

import org.apache.dubbo.common.extension.SPI;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;

import com.alibaba.fastjson.JSON;
import com.smf.lsc.security.converter.MyUserDetails;
import com.smf.lsc.security.util.AuthUserUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * c消费者拦截器，调用远程服务前，隐式传入用户信息
 * @author ln
 *
 */
@Slf4j
@SPI
public class ConsumerRpcFilter implements Filter {
	
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
    	MyUserDetails myUser=AuthUserUtil.getCurrentUser();
    	if(myUser.getAccount()!=null)
    	{
	    	String currentUserJson=JSON.toJSONString(myUser);
	        RpcContext.getContext().setAttachment(AuthUserUtil.USER_INFO_KEY,currentUserJson);
	        log.debug("MSRF-Dubbo Debug Message: 传入用户信息成功");
    	}
    	else
    	{
    		log.debug("MSRF-Dubbo Debug Message: 调用前无法取得用户信息【请检查当前服务用户信息获取渠道是否正常】");
    	}
        return invoker.invoke(invocation);
    }
}